<?php

 
class CV_Contact_Widget extends WP_Widget {
	
	var $prefix;
	var $textdomain;
	
	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.7
	 */
	function CV_Contact_Widget() {
		$this->prefix = 'cv';
		$this->textdomain = hybrid_get_textdomain();	
		
		$widget_ops = array('classname' => 'widget-contact', 'description' => __('Contact info.', $this->textdomain) );
		$this->WP_Widget("{$this->prefix}-contact-widget", __('Contact info', $this->textdomain), $widget_ops); 		
	}
	
	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 */
	function widget($args, $instance) {  
		extract( $args );
		
		echo $before_widget;

		/* If there is a title given, add it along with the $before_title and $after_title variables. */
		$title = $instance['title'];
		if ( $title) {
			$title =  apply_filters( 'widget_title',  $title, $instance, $this->id_base );
			$title = str_replace('contact', '<span>contact<span>r</span></span>', $title);
			echo $before_title . $title . $after_title;
		}	
				
		$email = $instance['email'];
		$address = $instance['address'];
		$telephone = $instance['telephone'];
		$skype = $instance['skype'];
		$twitter = $instance['twitter'];
		 ?>	
		<div class="contact-info">
			<ul>
				<?php if(!empty($email)) echo '<li class="email">' . $email. '</li>';  ?>
				<?php if(!empty($address)) echo '<li class="address">' . $address. '</li>';  ?>
				<?php if(!empty($telephone)) echo '<li class="telephone">' . $telephone. '</li>';  ?>
				<?php if(!empty($skype)) echo '<li class="skype">' . $skype. '</li>';  ?>
				<?php if(!empty($twitter)) echo '<li class="twitter">' . $twitter. '</li>';  ?>
			</ul>	
		</div>	
		
		<?php
		
	   echo $after_widget;
   }
	
	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.7
	 */
	function update($new_instance, $old_instance) {                
       return $new_instance;
   }
	
	/**
	 * Displays the widget control options in the Widgets admin screen.	
	 */
	function form($instance) {  
		$defaults = array(
			'title' => 'Contact Info',			
			'email' => '',			
			'address' => '',
			'telephone' => '',
			'skype' => '',
			'twitter' => ''
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); 		
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php _e( 'Email:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'email' ); ?>" name="<?php echo $this->get_field_name( 'email' ); ?>" value="<?php echo esc_attr( $instance['email'] ); ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'address' ); ?>"><?php _e( 'Address:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'address' ); ?>" name="<?php echo $this->get_field_name( 'address' ); ?>" value="<?php echo esc_attr( $instance['address'] ); ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'telephone' ); ?>"><?php _e( 'Telephone:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'telephone' ); ?>" name="<?php echo $this->get_field_name( 'telephone' ); ?>" value="<?php echo esc_attr( $instance['telephone'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'skype' ); ?>"><?php _e( 'Skype:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'skype' ); ?>" name="<?php echo $this->get_field_name( 'skype' ); ?>" value="<?php echo esc_attr( $instance['skype'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'twitter' ); ?>"><?php _e( 'Twitter:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'twitter' ); ?>" name="<?php echo $this->get_field_name( 'twitter' ); ?>" value="<?php echo esc_attr( $instance['twitter'] ); ?>" />
		</p>		
		
       <?php
	}

}
 
add_action('widgets_init', 'register_cv_contact_widget');

function register_cv_contact_widget() {
	register_widget('CV_Contact_Widget');
}